package bavaria.hightech.time;


import java.util.Calendar;

import java.util.Timer;
import java.util.TimerTask;


import bavaria.hightech.exception.NoMoneyEcxeption;


public class InteranlClock {

	private static InteranlClock instance;
	private Calendar c; 
	private final int faktor;
	private Clock clock;

	
	private TimerTasks callBackObjekt = null;
	
	public void buildTimerTask(TimerTasks callBackObj){
		callBackObjekt = callBackObj;
	}

	
	private InteranlClock(){
		this.c = Calendar.getInstance();
		this.faktor = 3600;
		this.clock = new Clock(c);
	
		
	}
	
	public synchronized static InteranlClock getInstanze(){
		if(instance == null)
			instance = new InteranlClock();
		
		return instance;
		
	}
	
	public Calendar getCalendar() {
		return (Calendar) c.clone();
	}

	public Calendar getDatum(){
		return c;
	}
	
	public void zeitSimulieren() {
		new TimerDemo().startTiming();
	}

	/**
	 * Unterklasse Timer
	 * @author Keks
	 *
	 */

	
	
	public class TimerDemo {

		private void simulierteZeit() throws NoMoneyEcxeption {
			c.add(Calendar.SECOND, faktor);
			clock.repaint();
			if ( c.get( Calendar.HOUR_OF_DAY) == 02){
			callBackObjekt.payIntrestAfterTime();	
			}
			
		}

		private void startTiming() {
			Timer timer = new Timer();
			timer.scheduleAtFixedRate(new TimerTask() {

				public void run() {
					try {
						TimerDemo.this.simulierteZeit();
					} catch (NoMoneyEcxeption e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}, 2000, 10);
		}

	}

}